<template>
  <div>
    <h1>拼团活动</h1>
  </div>
  <!-- <el-form :model="searchForm" :inline="true">
    <el-form-item label="关键字">
      <el-input v-model="searchForm.keyWord"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">搜索</el-button>
    </el-form-item>
  </el-form> -->
  <el-button type="primary" @click="addGroup()">添加拼团</el-button>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="activeId" label="活动id" width="180" />
      <el-table-column prop="activeName" label="活动名称" width="180" />
      <el-table-column prop="beginTime" label="活动名称" width="180" />
      <el-table-column prop="endTime" label="活动名称" width="180" />

      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success" @click="editForm()">编辑</el-button>
          <el-button type="danger" @click="deleteById(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="searchForm.pageNum"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <el-dialog v-model="dialogFormVisible" title="Shipping address" width="500">
    <el-form :model="saveForm">
      <el-form-item label="活动名称" :label-width="formLabelWidth">
        <el-input v-model="saveForm.activeName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="拼团人数" :label-width="formLabelWidth">
        <el-input v-model="saveForm.groupNum" autocomplete="off" />
      </el-form-item>
      <el-form-item label="开始时间">
        <div class="block">
          <el-date-picker
            v-model="saveForm.beginTime"
            type="datetime"
            placeholder="Select date and time"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </div>
      </el-form-item>
      <el-form-item label="结束时间">
        <div class="block">
          <el-date-picker
            v-model="saveForm.endTime"
            type="datetime"
            placeholder="Select date and time"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="saveGroup()"> 确认添加 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
const tableData = ref([]);
import { groupActive, saveGroupFun } from "@/api/api";
import { ElMessage } from "element-plus";
const searchForm = ref({
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
});
const total = ref(0);
const background = ref(false);
const disabled = ref(false);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  LoadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  LoadList();
};

const LoadList = () => {
  groupActive(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  LoadList();
});
//添加拼团
const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const saveForm = ref({
  activeName: "",
  beginTime: "",
  endTime: "",
  groupNum: 0,
});
const addGroup = () => {
  saveForm.value = {
    activeName: "",
    beginTime: "",
    endTime: "",
    groupNum: 0,
  };
  dialogFormVisible.value = true;
};

const saveGroup = () => {
  saveGroupFun(saveForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      LoadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const deleteById = () => {};
const editForm = () => {};
</script>

<style scoped></style>
